package cn.imu.appdevexperiment2020.work04;

import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.utils.ActivityUtil;
import cn.imu.appdevexperiment2020.utils.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        findViewById(R.id.register).setOnClickListener((view -> {
            Map<String, String> mp = new HashMap<>();

            EditText viewById = findViewById(R.id.editTextTextPersonName2);

            mp.put("userId", "77");
            mp.put("userName", viewById.getText().toString());
            mp.put("password", "123456");
            mp.put("nickName", "胡XX测试");
            mp.put("sex", "0");
            mp.put("status", "0");
            mp.put("updateBy", "appname");
            mp.put("phonenumber", "15614177688");
            HttpUtil.post(HttpUtil.ADDR + "/app/user/api/register", mp, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    // Toast.makeText(this,"注册失败未知错误",Toast.LENGTH_LONG).show();
                    System.out.println("注册失败未知错误");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    JSONObject jsonObject = JSON.parseObject(response.body().string());
                    if (jsonObject.get("code").toString().equals("200")) {
                        //说明解析成功跳转界面
                        ActivityUtil.getInstance().goActivity(RegisterActivity.this, LoginActivity.class);

                    } else {
                        // System.out.println(jsonObject.get("code"));
                        System.out.println("注册失败");
                    }


                }
            });


        }));
    }
}